﻿using Cheems.Interface.Models.UserManagement;
using Google.Protobuf.Compiler;
using Prism.Mvvm;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;

namespace Cheems.Control.Models.Chat
{
    public class ChatHistory : BindableBase, IComparable<ChatHistory>
    {
        public ChatHistory()
        {
            User = new T_User();
            Message = string.Empty;
            FlowDirection = FlowDirection.LeftToRight;
        }

        private T_User _user;
        /// <summary>
        /// 用户
        /// </summary>
        public T_User User
        {
            get { return _user; }
            set { SetProperty(ref _user, value); }
        }

        private string _message;
        /// <summary>
        /// 消息
        /// </summary>
        public string Message
        {
            get { return _message; }
            set { SetProperty(ref _message, value); }
        }

        public int CompareTo(ChatHistory other)
        {
            if (other == null) return 1;

            if (this.User == null) return -1;
            if (other.User == null) return 1;

            // 直接比较 DateTime 类型的 CreatTime
            return this.User.CreatTime.CompareTo(other.User.CreatTime);
        }

        private FlowDirection _flowDirection;
        /// <summary>
        /// 方向
        /// </summary>
        public FlowDirection FlowDirection
        {
            get { return _flowDirection; }
            set { SetProperty(ref _flowDirection, value); }
        }

        // 重写 Equals 方法
        public override bool Equals(object obj)
        {
            if (obj is ChatHistory other)
            {
                // 判断 User 和 Message 是否相等
                return this.User.Equals(other.User) && this.Message == other.Message;
            }

            return false;
        }

        // 重写 GetHashCode 方法
        public override int GetHashCode()
        {
            // 使用 User 和 Message 的哈希值生成新的哈希值
            int hashUser = User?.GetHashCode() ?? 0;
            int hashMessage = Message?.GetHashCode() ?? 0;
            return hashUser ^ hashMessage; // 使用异或运算符组合哈希值
        }

        // 可选：重写 == 和 != 运算符
        public static bool operator ==(ChatHistory left, ChatHistory right)
        {
            return Equals(left, right);
        }

        public static bool operator !=(ChatHistory left, ChatHistory right)
        {
            return !Equals(left, right);
        }
    }

}
